package com.xq.jltx.serverapp;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xq.jltx.common.Constants;
import com.xq.jltx.utils.XqToast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText ipEditText;
    private EditText postEditText;
    private TextView currentTv;
    private Button startBtn;
    private Button stopBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignView();
        startBtn.setOnClickListener(this);
        stopBtn.setOnClickListener(this);
    }

    private void assignView(){
        ipEditText = (EditText)this.findViewById(R.id.ipEdit);
        postEditText = (EditText)this.findViewById(R.id.postEdit);
        currentTv = (TextView)this.findViewById(R.id.currentTv);

        startBtn = (Button)this.findViewById(R.id.startBtn);
        stopBtn = (Button)this.findViewById(R.id.stopBtn);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.startBtn:
                String ipStr = ipEditText.getText().toString();
                if(TextUtils.isEmpty(ipStr)){
                    XqToast.showToastShortMsg(this,"请正确填写ip地址");
                    return;
                }

                String postStr = postEditText.getText().toString();
                if(TextUtils.isEmpty(postStr)){
                    XqToast.showToastShortMsg(this,"请正确填写端口号");
                    return;
                }
                startBtn.setEnabled(false);
                stopBtn.setEnabled(true);
                break;
            case R.id.stopBtn:
                startBtn.setEnabled(true);
                stopBtn.setEnabled(false);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences p = getSharedPreferences(Constants.SERVICE_STATUS, MODE_PRIVATE);
        boolean started = p.getBoolean(Constants.PREF_STARTED, false);
        startBtn.setEnabled(!started);
        stopBtn.setEnabled(started);
    }
}
